package com.nhc.activity_fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.astrob.nhcmap.R;

import java.text.DecimalFormat;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.util.StringUtil;

public class EditNumberActivity extends BaseActivity implements View.OnClickListener {
    private final String TAG = "EditNumberActivity";
    static final String INTENT_NUMBER_T = "INTENT_NUMBER_T";
    static final String INTENT_NUMBER_C = "INTENT_NUMBER_C";
    static final String INTENT_NUMBER_U = "INTENT_NUMBER_U";

    static final String INTENT_NUMBER_D = "INTENT_NUMBER_D";

    static final String INTENT_NUMBER_J = "INTENT_NUMBER_J";

    static final String INTENT_NUMBER_MAX = "INTENT_NUMBER_MAX";
    static final String INTENT_NUMBER_MIN = "INTENT_NUMBER_MIN";

    static final String INTENT_NUMBER_MAXD = "INTENT_NUMBER_MAXD";
    static final String INTENT_NUMBER_MIND = "INTENT_NUMBER_MIND";
    //启动方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    /**
     * 启动这个Activity的Intent
     *
     * @param context
     * @return
     */
    public static Intent createIntent(Context context, String title, int number, String unit) {
        return new Intent(context, EditNumberActivity.class)
                .putExtra(INTENT_NUMBER_T, title)
                .putExtra(INTENT_NUMBER_C, number)
                .putExtra(INTENT_NUMBER_U, unit);
    }

    public static Intent createIntentLimt(Context context, String title, int number, String unit, int min, int max) {
        return new Intent(context, EditNumberActivity.class)
                .putExtra(INTENT_NUMBER_T, title)
                .putExtra(INTENT_NUMBER_MIN, min)
                .putExtra(INTENT_NUMBER_MAX, max)
                .putExtra(INTENT_NUMBER_C, number)
                .putExtra(INTENT_NUMBER_U, unit);
    }

    public static Intent createIntentD(Context context, String title, double number, String unit) {
        return new Intent(context, EditNumberActivity.class)
                .putExtra(INTENT_NUMBER_T, title)
                .putExtra(INTENT_NUMBER_D, number)
                .putExtra(INTENT_NUMBER_U, unit);
    }

    //可以输入负数
    public static Intent createIntentDLimt(Context context, String title, double number, String unit, double minD, double maxD) {
        return new Intent(context, EditNumberActivity.class)
                .putExtra(INTENT_NUMBER_T, title)
                .putExtra(INTENT_NUMBER_MIND, minD)
                .putExtra(INTENT_NUMBER_MAXD, maxD)
                .putExtra(INTENT_NUMBER_D, number)
                .putExtra(INTENT_NUMBER_U, unit);
    }

    //坡度角 0～90
    public static Intent createIntentJ(Context context, String title, int number, String unit) {
        return new Intent(context, EditNumberActivity.class)
                .putExtra(INTENT_NUMBER_T, title)
                .putExtra(INTENT_NUMBER_C, number)
                .putExtra(INTENT_NUMBER_J, true)
                .putExtra(INTENT_NUMBER_U, unit);
    }

    //启动方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    int _number = 0;
    String _title = "";
    String _unit = "";
    double _numberD = 666666;
    boolean _isDouble = false;

    boolean _isJaodu = false;
    int _max = 999999999;//2147483647
    int _min = -999999999;

    double _minD = -90;
    double _maxD = 0;

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.editnumber_activity);

        intent = getIntent();
        _title = intent.getStringExtra(INTENT_NUMBER_T);
        _number = intent.getIntExtra(INTENT_NUMBER_C, _number);
        _unit = intent.getStringExtra(INTENT_NUMBER_U);
        _numberD = intent.getDoubleExtra(INTENT_NUMBER_D, _numberD);

        _isJaodu = intent.getBooleanExtra(INTENT_NUMBER_J, _isJaodu);

        _max = intent.getIntExtra(INTENT_NUMBER_MAX, _max);
        _min = intent.getIntExtra(INTENT_NUMBER_MIN, _min);

        if (_numberD == 666666) {
            _isDouble = false;
        } else {
            _isDouble = true;
            _minD = intent.getDoubleExtra(INTENT_NUMBER_MIND, _minD);
            _maxD = intent.getDoubleExtra(INTENT_NUMBER_MAXD, _maxD);
        }

        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //功能归类分区方法，必须调用>>>>>>>>>>

    }

    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    TextView tvUnit;
    EditText etEditTextInfo;
    ImageView ivEditTextInfoClear;
    TextView tvEditTextInfoRemind;

    @Override
    public void initView() {//必须调用
        ivEditTextInfoClear = (ImageView) findViewById(R.id.ivEditTextInfoClear);
        tvUnit = (TextView) findViewById(R.id.tvUnit);
        etEditTextInfo = (EditText) findViewById(R.id.etEditTextInfo);
        tvEditTextInfoRemind = (TextView) findViewById(R.id.tvEditTextInfoRemind);
        if (_title != null) {
            tvBaseTitle.setText(_title);
        }

        etEditTextInfo.setText(_number + "");
        //要显示 -
        etEditTextInfo.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);

        if (_isDouble) {
            if (_numberD != 0) {
                etEditTextInfo.setText(new DecimalFormat("0.00000000").format(_numberD));
            }

            if (_maxD > 0) {
                tvEditTextInfoRemind.setText(getString(R.string.qsrsz) + "(" + (int) _minD + "~" + (int) _maxD + ")");
            }
        } else if (_max != 999999999) {
            if (_max > 0) {
                tvEditTextInfoRemind.setText(getString(R.string.qsrsz) + "(0~" + _max + ")");
            }
        }
        tvUnit.setText(_unit);

    }

    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    @Override
    public void initData() {//必须调用

    }


    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    private String inputedString;

    @Override
    public void initEvent() {//必须调用
        etEditTextInfo.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                inputedString = StringUtil.getTrimedString(s);

                if (StringUtil.isNotEmpty(inputedString, true) == false) {
                    ivEditTextInfoClear.setVisibility(View.GONE);
                } else {
                    ivEditTextInfoClear.setVisibility(View.VISIBLE);
                }
                String temp = "";
                if (_isDouble) {
                    temp = StringUtil.getNumberD(inputedString);
                    int size = inputedString.length();
                    if (_maxD > 0 && size > 1) {
                        try {
                            double x = Double.valueOf(temp);
                            if (x > _maxD) {
                                temp = (int)(x/10) + "";
                            }
                            if (x < _minD) {
                                temp = (int)(x/10) + "";
                            }
                        } catch (Exception e) {

                        }
                    }
                } else {
                    temp = StringUtil.getNumber(inputedString);
                    int size = inputedString.length();
                    if (_max > 0 && size > 1) {
                        try {
                            double x = Double.valueOf(temp);
                            if (x > _max) {
                                temp = temp.substring(0, size - 1);
                            }
                        } catch (Exception e) {

                        }
                    }
                }

                if (temp.compareToIgnoreCase(inputedString) != 0) {
                    etEditTextInfo.setText(temp);
                    int index = temp.length();
                    etEditTextInfo.setSelection(index);
                }

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable edt) {

            }
        });

        ivEditTextInfoClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                etEditTextInfo.setText("");
            }
        });

    }
    //系统自带监听方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    //类相关监听<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    @Override
    public void finish() {
        super.finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {

    }

    public static final String RESULT_TYPE = "RESULT_TYPE";
    public static final String RESULT_VALUE = "RESULT_VALUE";

    @Override
    public void onForwardClick(View v) {
        String editedValue = StringUtil.getTrimedString(etEditTextInfo);

        try {
            double xx = Double.valueOf(editedValue);
            if (_isJaodu && (xx >= 90 || xx < 0)) {
                showShortToast(getString(R.string.pdjydylxyjsd));
                etEditTextInfo.setText("0");
                return;
            }

        } catch (Exception e) {
            showShortToast(getString(R.string.qsrsz));
            return;
        }


        intent = new Intent();
        intent.putExtra(RESULT_VALUE, editedValue);
        setResult(RESULT_OK, intent);

        exitAnim = zuo.biao.library.R.anim.left_push_out;
        finish();
    }

    //类相关监听>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //系统自带监听方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //内部类,尽量少用<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    //内部类,尽量少用>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

}
